/**
 * Copyright (c) 2024 - CARThunder
 * All Rights Reserved by Chengdu CARThunder Intelligent Technology Co., LTD and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with CARThunder in writing by applicable law.
 *
 */

/**
 * file description.
 * @file    filename
 * @brief   brief function description.
 * @details detailed function description.
 * @version 1.0
 * @author  author_name
 * @date    last_modified_date
 *
 *      Edit History
 *      ----------------------------------------------------------------------------
 *      DATE                     NAME               DESCRIPTION
 *      2024-07-30              junjieluo         Create it.
 *
 */



package com.cart.musicdemo.base.activity;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * The type Base adapter.
 *
 * @param <E> the type parameter
 * @param <V> the type parameter
 */
public abstract class BaseAdapter<E, V extends RecyclerView.ViewHolder>
        extends RecyclerView.Adapter<V> {
    /**
     * The M context.
     */
    protected Context mContext;
    /**
     * The M list.
     */
    protected List<E> mList;
    /**
     * The M item click listener.
     */
    protected OnItemClickListener mItemClickListener;
    /**
     * The M on item long click listener.
     */
    protected OnItemLongClickListener mOnItemLongClickListener;

    /**
     * Instantiates a new Base adapter.
     *
     * @param context the context
     */
    public BaseAdapter(Context context) {
        mContext = context;
        mList = new ArrayList<E>();
    }

    @NonNull
    @Override
    public V onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        int layoutid = onBindLayout();
        View view = LayoutInflater.from(mContext).inflate(layoutid, parent, false);
        return onCreateHolder(view);
    }

    /**
     * On bind layout int.
     *
     * @return the int
     */
    protected abstract int onBindLayout();

    /**
     * On create holder v.
     *
     * @param view the view
     * @return the v
     */
    protected abstract V onCreateHolder(View view);

    /**
     * On bind data.
     *
     * @param holder  the holder
     * @param e       the e
     * @param positon the positon
     */
    protected abstract void onBindData(V holder, E e, int positon);

    @Override
    public void onBindViewHolder(@NonNull V holder, final int position) {
        final E e = mList.get(position);
        if (mItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mItemClickListener.onItemClick(e, position);
                }
            });
        }
        if (mOnItemLongClickListener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    return mOnItemLongClickListener.onItemLongClick(e, position);
                }
            });
        }
        onBindData(holder, e, position);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    /**
     * Add all.
     *
     * @param list the list
     */
    public void addAll(List<E> list) {
        if (list != null && list.size() > 0) {
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    /**
     * Refresh.
     *
     * @param list the list
     */
    public void refresh(List<E> list) {
        mList.clear();
        if (list != null && list.size() > 0) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    /**
     * Remove.
     *
     * @param positon the positon
     */
    public void remove(int positon) {
        mList.remove(positon);
        notifyDataSetChanged();
    }

    /**
     * Remove.
     *
     * @param object the object
     */
    public void remove(E object) {
        mList.remove(object);
        notifyDataSetChanged();
    }

    /**
     * Add.
     *
     * @param object the object
     */
    public void add(E object) {
        mList.add(object);
        notifyDataSetChanged();
    }

    /**
     * Add last.
     *
     * @param object the object
     */
    public void addLast(E object) {
        add(object);
    }

    /**
     * Add first.
     *
     * @param object the object
     */
    public void addFirst(E object) {
        mList.add(0, object);
        notifyDataSetChanged();
    }

    /**
     * Clear.
     */
    public void clear() {
        mList.clear();
        notifyDataSetChanged();
    }

    /**
     * Gets data list.
     *
     * @return the data list
     */
    public List<E> getDataList() {
        return mList;
    }

    /**
     * Sets item click listener.
     *
     * @param itemClickListener the item click listener
     */
    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    /**
     * Sets on item long click listener.
     *
     * @param onItemLongClickListener the on item long click listener
     */
    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        mOnItemLongClickListener = onItemLongClickListener;
    }

    /**
     * The interface On item click listener.
     *
     * @param <E> the type parameter
     */
    public interface OnItemClickListener<E> {
        /**
         * On item click.
         *
         * @param object   the object
         * @param position the position
         */
        void onItemClick(E object, int position);
    }

    /**
     * The interface On item long click listener.
     *
     * @param <E> the type parameter
     */
    public interface OnItemLongClickListener<E> {
        /**
         * On item long click boolean.
         *
         * @param object  the object
         * @param postion the postion
         * @return the boolean
         */
        boolean onItemLongClick(E object, int postion);
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    /**
     * Gets list data.
     *
     * @return the list data
     */
    public List<E> getListData() {
        return mList;
    }
}
